package com.ivncorp.evnproject;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ivncorp.evnproject.api.JSONParser;
import com.ivncorp.evnproject.api.RestAPI;
import com.ivncorp.evnproject.helpers.InternetConnectionChecker;
import com.ivncorp.evnproject.helpers.Variables;

public class ActivityLogin extends Activity {

	EditText etUserName, etPassword;
	Button btnLogin;
	Context context;
	Boolean Login_success = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		context = this;
		etUserName = (EditText) findViewById(R.id.etUserName);
		etPassword = (EditText) findViewById(R.id.etPass);
		btnLogin = (Button) findViewById(R.id.btnSingIn);

		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String username = etUserName.getText().toString();
				String password = etPassword.getText().toString();

				// Execute the AsyncLogin class
				if (InternetConnectionChecker
						.isNetworkConnected(getApplicationContext())) {
					new AsyncLogin().execute(username, password);
				} else {
					Toast.makeText(
							getApplicationContext(),
							"Không có kết nối với Internet, vui lòng kết nối Internet.",
							Toast.LENGTH_SHORT).show();
				}

			}
		});

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		savingPreferences();
		super.onPause();
	}

	void savingPreferences() {
		SharedPreferences pre = getSharedPreferences("Login_Setting",
				MODE_PRIVATE);
		SharedPreferences.Editor editor = pre.edit();
		editor.putBoolean("Login_Success", Login_success);
		editor.commit();
	}

	private ProgressDialog dialog;
	protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean> {

		@Override
		protected void onPreExecute() {

			dialog = new ProgressDialog(ActivityLogin.this);
			dialog.setMessage(getResources().getString(R.string.Loginning));
			dialog.setIndeterminate(false);
			dialog.setCancelable(false);
			dialog.show();
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {

			RestAPI api = new RestAPI();
			boolean userAuth = false;
			try {

				// Call the User Authentication Method in API
				JSONObject jsonObj = api.UserAuthentication(params[0],
						params[1]);

				// Parse the JSON Object to boolean
				JSONParser parser = new JSONParser();
				userAuth = parser.parseUserAuth(jsonObj);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.d("AsyncLogin", e.getMessage());

			}
			return userAuth;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			dialog.dismiss();

			// Check user validity
			if (result) {
				Intent i = new Intent(ActivityLogin.this, ActivityMain.class);
				Login_success = true;
				Variables.userName = etUserName.getText().toString();
				finish();
				startActivity(i);
			} else {
				Toast.makeText(context, R.string.login_fail,
						Toast.LENGTH_LONG).show();
			}

		}

	}

}
